<template>
  <div class="login">
    <!-- <div class="login_show"></div> -->
    <div class="left">
      <div class="top">
        <img class="logo" src="@/assets/img/login_logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <div class="form">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-row>
              <el-col :span="18">
                <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
              </el-col>
              <el-col :span="6">
                <img class="code" :src="codeUrl" @click="codeClick" alt />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="checked">
            <el-checkbox v-model="form.checked" :lable="true">
              我已阅读并同意
              <el-link type="primary">用户协议</el-link>和
              <el-link type="primary">隐私条款</el-link>
            </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="btn" @click="tologin">登入</el-button>
            <br />
            <el-button type="primary" class="btn" @click="toregister" ref="register">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
      <register ref="register"></register>
    </div>
    <img src="@/assets/img/login_img.png" alt class="right" />
    <!-- <div class="login_hide"></div> -->
  </div>
</template>
<script>
import register from "@/views/login/register.vue";
import { loginApi } from "@/Api/login.js";
import { setToken, getToken } from "@/utils/local.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      // codeUrl: require("@/assets/img/login_code.png"),
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        checked: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                  value
                )
              ) {
                callback();
              } else {
                callback(new Error("请正确输入手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "请输入6-12位的密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位的验证码", trigger: "change" },
        ],
        checked: [{ required: true, message: "勾选协议", trigger: "change" }],
      },
    };
  },
  methods: {
    codeClick() {
      this.codeUrl =
        process.env.VUE_APP_URL +
        "/captcha?type=login&sffsd" +
        Math.random() * 999;
    },
    tologin() {
      this.$refs.form.validate((result) => {
        if (result) {
          // this.$message.success("验证成功");
          loginApi(this.form)
            .then((res) => {
              // window.console.log(res);
              // this.$message.success("登入成功");
              setToken("token", res.data.data.token);
              this.$router.push("/layout");
            })
            .catch((err) => err);
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    toregister() {
      this.$refs.register.isShow = true;
      this.$refs.register.codeUrl =
        process.env.VUE_APP_URL +
        "/captcha?type=sendsms&sffsd" +
        Math.random() * 999;
    },
  },
  created() {
    // window.console.log(getToken("token"));
    if (getToken("token")) {
      this.$router.push("/layout");
    }
  },
};
</script>
<style lang="less">
.login {
  display: flex;
  align-items: center;
  justify-content: space-around;
  // position: relative;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);

  .left {
    padding: 42px;
    padding-top: 48px;
    width: 480px;
    height: 550px;
    background: #f5f5f5;

    .top {
      display: flex;
      align-items: center;
      margin-bottom: 28px;
      .logo {
        width: 22px;
        height: 18px;
        margin-right: 16px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .line {
        margin: 0 14px;
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
    }
    .form {
      .btn {
        width: 100%;
        margin-bottom: 26px;
      }
      .code {
        height: 40px;
        width: 100%;
      }
    }
  }
  // .login_hide {
  //   background: #000;
  //   position: absolute;
  //   top: 0;
  //   left: 0;
  //   display: none;
  //   width: 100%;
  //   height: 100%;
  //   opacity: 0.3;
  // }
}
</style>
